/**
 *
 */
package de.cbf.cam.dsa.facet.item.editor;

import java.beans.IndexedPropertyChangeEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.widgets.Control;

import de.cbf.cam.dsa.facet.item.FacetItem;
import de.cbf.cam.dsa.facet.item.editor.shapeness.CompositeEditShapenessDescriptions;
import de.cbf.cam.dsa.facet.item.editor.shapeness.ControllerEditShapenessDescriptions;
import de.cbf.cam.dsa.facet.item.helper.Shapeness;
import de.cbf.util.controller.ControllerEvent;
import de.cbf.util.controller.IListenerController;
import de.cbf.util.entity.facet.edit.composites.factorys.AbstractFacetSectionFactory;
import de.cbf.util.entity.facet.edit.controller.AbstractControllerEditFacet;
import de.cbf.util.ui.listener.IUiChangeListener;
import de.cbf.util.ui.listener.UiChangeEvent;

/**
 * @author Nebelritter
 *
 */
public class ControllerEditorFacetItem extends AbstractControllerEditFacet<FacetItem> {
	private static final Log logger = LogFactory.getLog(ControllerEditorFacetItem.class);

	public static final String TEXT_STATE_EVENT_Weight_ERROR				= "Weight setting not correct";
	public static final String TEXT_STATE_EVENT_Weight_WARNING				= FacetItem.TEXT_UNSET_WEIGHT;
	public static final String TEXT_STATE_EVENT_Description_ERROR			= "Description setting not correct";
	public static final String TEXT_STATE_EVENT_Description_WARNING			= FacetItem.TEXT_UNSET_DESCRIPTION;
	public static final String TEXT_STATE_EVENT_Type_ERROR					= "Type setting not correct";
	public static final String TEXT_STATE_EVENT_Type_WARNING				= FacetItem.TEXT_UNSET_TYPE;
	public static final String TEXT_STATE_EVENT_ValueKreuzer_ERROR			= "ValueKreuzer setting not correct";
	public static final String TEXT_STATE_EVENT_ValueKreuzer_WARNING		= FacetItem.TEXT_UNSET_VALUE_KREUZER;
	public static final String TEXT_STATE_EVENT_QualityMin_ERROR			= "QualityMin setting not correct";
	public static final String TEXT_STATE_EVENT_QualityMin_WARNING			= FacetItem.TEXT_UNSET_QUALITY_MIN;
	public static final String TEXT_STATE_EVENT_QualityMax_ERROR			= "QualityMax setting not correct";
	public static final String TEXT_STATE_EVENT_QualityMax_WARNING			= FacetItem.TEXT_UNSET_QUALITY_MAX;
	public static final String TEXT_STATE_EVENT_BrokennessLevels_WARNING	= FacetItem.TEXT_UNSET_BROKENNESS_DESCRIPTIONS;	
	public static final String TEXT_STATE_EVENT_DirtinessLevels_WARNING		= FacetItem.TEXT_UNSET_DIRTINESS_DESCRIPTIONS;	
	
	protected static StateEvent STATE_EVENT_Weight_ERROR 				= new StateEvent(TEXT_STATE_EVENT_Weight_ERROR, StateEvent.STATE_ERROR); 	
	protected static StateEvent STATE_EVENT_Weight_WARNING				= new StateEvent(TEXT_STATE_EVENT_Weight_WARNING, StateEvent.STATE_WARNING); 
	protected static StateEvent STATE_EVENT_Description_ERROR 			= new StateEvent(TEXT_STATE_EVENT_Description_ERROR, StateEvent.STATE_ERROR); 	
	protected static StateEvent STATE_EVENT_Description_WARNING			= new StateEvent(TEXT_STATE_EVENT_Description_WARNING, StateEvent.STATE_WARNING); 
	protected static StateEvent STATE_EVENT_Type_ERROR 					= new StateEvent(TEXT_STATE_EVENT_Type_ERROR, StateEvent.STATE_ERROR); 	
	protected static StateEvent STATE_EVENT_Type_WARNING				= new StateEvent(TEXT_STATE_EVENT_Type_WARNING, StateEvent.STATE_WARNING); 
	protected static StateEvent STATE_EVENT_ValueKreuzer_ERROR 			= new StateEvent(TEXT_STATE_EVENT_ValueKreuzer_ERROR, StateEvent.STATE_ERROR); 	
	protected static StateEvent STATE_EVENT_ValueKreuzer_WARNING		= new StateEvent(TEXT_STATE_EVENT_ValueKreuzer_WARNING, StateEvent.STATE_WARNING); 
	protected static StateEvent STATE_EVENT_QualityMin_ERROR 			= new StateEvent(TEXT_STATE_EVENT_QualityMin_ERROR, StateEvent.STATE_ERROR); 	
	protected static StateEvent STATE_EVENT_QualityMin_WARNING			= new StateEvent(TEXT_STATE_EVENT_QualityMin_WARNING, StateEvent.STATE_WARNING); 
	protected static StateEvent STATE_EVENT_QualityMax_ERROR 			= new StateEvent(TEXT_STATE_EVENT_QualityMax_ERROR, StateEvent.STATE_ERROR); 	
	protected static StateEvent STATE_EVENT_QualityMax_WARNING			= new StateEvent(TEXT_STATE_EVENT_QualityMax_WARNING, StateEvent.STATE_WARNING); 
	protected static StateEvent STATE_EVENT_BrokennessLevels_WARNING	= new StateEvent(TEXT_STATE_EVENT_BrokennessLevels_WARNING, StateEvent.STATE_WARNING);
	protected static StateEvent STATE_EVENT_DirtinessLevels_WARNING		= new StateEvent(TEXT_STATE_EVENT_DirtinessLevels_WARNING, StateEvent.STATE_WARNING);
	
	public static final String BROKENESS_ADD_DIALOG_TITLE	= "Add brokenness..";
	public static final String BROKENESS_ADD_DIALOG_MESSAGE = "Add a brokenness level and sort it in";
	public static final String DIRTINESS_ADD_DIALOG_TITLE 	= "Add dirtiness..";
	public static final String DIRTINESS_ADD_DIALOG_MESSAGE = "Add a dirtiness level and sort it in";

	protected CompositeEditItem compositeEditItem;
	//singleton sub-controllers
	protected ControllerEditShapenessDescriptions ctrl_EditBrokenness;
	protected ControllerEditShapenessDescriptions ctrl_EditDirtiness;

	protected PCLFacetItem pclFacetItem;
	protected UILFacetItem uilFacetItem;

	protected CELEditShapenessDescriptionsBrokenness celBrokenness;

	protected CELEditShapenessDescriptionsDirtiness celDirtiness;

	protected NumberFormat format;	
	protected NumberFormat nf_valueKreuzer;
	
	/**
	 *
	 */
	public ControllerEditorFacetItem() {

	}

	@Override
	protected void init() {
		super.init();		
		pclFacetItem 	= new PCLFacetItem();
		uilFacetItem 	= new UILFacetItem();
		format			= NumberFormat.getInstance();		
		nf_valueKreuzer = NumberFormat.getIntegerInstance();
		nf_valueKreuzer.setGroupingUsed(false);
	}


	@Override
	protected AbstractFacetSectionFactory createSectionFactory() {
		return new SectionFactoryItem();
	}

	@Override
	protected FacetItem createNewFacet() {
		return new FacetItem();
	}

	@Override
	public Class<FacetItem> getFacetClass() {
		return FacetItem.class;
	}
	
	
	
	
	public void modifyWeight(String newWeight) {	
		if(facet == null) return;	
		Number valueNumber;
		try {
			if(compositeEditItem != null){
				compositeEditItem.setDecorationWeightStatus(false);
			}
			removeStateEvent(STATE_EVENT_Weight_ERROR);
			valueNumber =  format.parse(newWeight);		
			if (valueNumber instanceof Long) {
				Long valueInteger = (Long) valueNumber;
				facet.setWeight(valueInteger.doubleValue());
			}
			if (valueNumber instanceof Double) {
				Double valueDouble = (Double) valueNumber;
				facet.setWeight(valueDouble);
			}
		} catch (ParseException e) {
			if(compositeEditItem != null){
				compositeEditItem.setDecorationWeightStatus(true);	
			}
			addStateEvent(STATE_EVENT_Weight_ERROR);			
		}	
	}
		
	public void modifyDescription(String newDescription) {	
		if(facet == null) return;	
		facet.setDescription(newDescription);		
	}
		
	public void modifyType(String newType) {	
		if(facet == null) return;	
		facet.setType(newType);		
	}

	public void modifyValueKreuzer(String newValueKreuzer) {	
		if(facet == null) return;	
		Number valueNumber;
		try {
			if(compositeEditItem != null){
				compositeEditItem.setDecorationValueKreuzerStatus(false);
			}
			removeStateEvent(STATE_EVENT_ValueKreuzer_ERROR);
			valueNumber =  nf_valueKreuzer.parse(newValueKreuzer);		
			if (valueNumber instanceof Long) {
				Long valueLong = (Long) valueNumber;
				facet.setValueKreuzer(valueLong);
			}
			if (valueNumber instanceof Integer) {
				Integer valueInteger = (Integer) valueNumber;
				facet.setValueKreuzer(valueInteger.longValue());
			}
		} catch (ParseException e) {
			if(compositeEditItem != null){
				compositeEditItem.setDecorationValueKreuzerStatus(true);	
			}
			addStateEvent(STATE_EVENT_ValueKreuzer_ERROR);			
		}	
	}
	
	public void modifyQualityMin(String newQualityMin) {	
		if(facet == null) return;	
		Number valueNumber;
		try {
			if(compositeEditItem != null){
				compositeEditItem.setDecorationQualityMinStatus(false);
			}
			removeStateEvent(STATE_EVENT_QualityMin_ERROR);
			valueNumber =  format.parse(newQualityMin);		
			if (valueNumber instanceof Long) {
				Long valueLong = (Long) valueNumber;
				facet.setQualityMin(valueLong.doubleValue());
			}
			if (valueNumber instanceof Double) {
				Double valueDouble = (Double) valueNumber;
				facet.setQualityMin(valueDouble);
			}
		} catch (ParseException e) {
			if(compositeEditItem != null){
				compositeEditItem.setDecorationQualityMinStatus(true);	
			}
			addStateEvent(STATE_EVENT_QualityMin_ERROR);			
		}	
	}
	
	public void modifyQualityMax(String newQualityMax) {	
		if(facet == null) return;	
		Number valueNumber;
		try {
			if(compositeEditItem != null){
				compositeEditItem.setDecorationQualityMaxStatus(false);
			}
			removeStateEvent(STATE_EVENT_QualityMax_ERROR);
			valueNumber =  format.parse(newQualityMax);		
			if (valueNumber instanceof Long) {
				Long valueLong = (Long) valueNumber;
				facet.setQualityMax(valueLong.doubleValue());
			}
			if (valueNumber instanceof Double) {
				Double valueDouble = (Double) valueNumber;
				facet.setQualityMax(valueDouble);
			}
		} catch (ParseException e) {
			if(compositeEditItem != null){
				compositeEditItem.setDecorationQualityMaxStatus(true);	
			}
			addStateEvent(STATE_EVENT_QualityMax_ERROR);			
		}	
	}
	
	/**
	 * @param list
	 */
	public void modifyBrokennessDescriptions(List<Object> list) {
		List<String> newDescriptions = reformObjectListToStringList(list);		
		if(facet != null && facet.getBrokenness() != null){
			facet.getBrokenness().setShapenessDescriptions(newDescriptions);
		}		
	}
	
	/**
	 * @param list
	 */
	public void modifyDirtinessDescriptions(List<Object> list) {
		List<String> newDescriptions = reformObjectListToStringList(list);
		if(facet != null && facet.getDirtiness() != null){
			facet.getDirtiness().setShapenessDescriptions(newDescriptions);
		}		
	}
	
	public void updateWeight(Double newWeight) {
		if(newWeight != null && compositeEditItem != null){
			if( ! newWeight.equals(FacetItem.UNSET_WEIGHT)){				
				compositeEditItem.setWeight(format.format(newWeight));
				removeStateEvent(STATE_EVENT_Weight_WARNING);
			}else{
				compositeEditItem.setWeight("");
				addStateEvent(STATE_EVENT_Weight_WARNING);
			}				
		}			
	}
	
	public void updateDescription(String newDescription) {
		if(newDescription != null && compositeEditItem != null){
			if( ! newDescription.equals(FacetItem.UNSET_DESCRIPTION)){				
				compositeEditItem.setDescription(newDescription);
				removeStateEvent(STATE_EVENT_Description_WARNING);
			}else{
				compositeEditItem.setDescription("");
				addStateEvent(STATE_EVENT_Description_WARNING);
			}				
		}			
	}

	public void updateType(String newType) {
		if( compositeEditItem != null){
			if(newType != null && ! newType.equals(FacetItem.UNSET_TYPE)){				
				compositeEditItem.setType(newType);
				removeStateEvent(STATE_EVENT_Type_WARNING);
			}else{
				compositeEditItem.setType("");
				addStateEvent(STATE_EVENT_Type_WARNING);
			}				
		}			
	}
	
	public void updateValueKreuzer(Long newValueKreuzer) {
		if(newValueKreuzer != null && compositeEditItem != null){
			if( ! newValueKreuzer.equals(FacetItem.UNSET_VALUE_KREUZER)){				
				compositeEditItem.setValueKreuzer(format.format(newValueKreuzer));
				removeStateEvent(STATE_EVENT_ValueKreuzer_WARNING);
			}else{
				compositeEditItem.setValueKreuzer("");
				addStateEvent(STATE_EVENT_ValueKreuzer_WARNING);
			}				
		}			
	}
	
	public void updateQualityMin(Double newQualityMin) {
		if(newQualityMin != null && compositeEditItem != null){
			if( ! newQualityMin.equals(FacetItem.UNSET_QUALITY_MIN)){				
				compositeEditItem.setQualityMin(format.format(newQualityMin));
				removeStateEvent(STATE_EVENT_QualityMin_WARNING);
			}else{
				compositeEditItem.setQualityMin("");
				addStateEvent(STATE_EVENT_QualityMin_WARNING);
			}				
		}			
	}
	
	public void updateQualityMax(Double newQualityMax) {
		if(newQualityMax != null && compositeEditItem != null){
			if( ! newQualityMax.equals(FacetItem.UNSET_QUALITY_MAX)){				
				compositeEditItem.setQualityMax(format.format(newQualityMax));
				removeStateEvent(STATE_EVENT_QualityMax_WARNING);
			}else{
				compositeEditItem.setQualityMax("");
				addStateEvent(STATE_EVENT_QualityMax_WARNING);
			}				
		}			
	}
	
	/**
	 * @param newBrokennessLevels
	 */
	public void updateBrokennessLevels(List<String> newBrokennessLevels) {
		if(newBrokennessLevels != null && ctrl_EditBrokenness != null){				
			ctrl_EditBrokenness.setList(newBrokennessLevels);
			if(newBrokennessLevels == null || newBrokennessLevels.isEmpty()){
				addStateEvent(STATE_EVENT_BrokennessLevels_WARNING);
			}else{
				removeStateEvent(STATE_EVENT_BrokennessLevels_WARNING);
			}
		}		
	}
	/**
	 * @param newDirtinessLevels
	 */
	public void updateDirtinessLevels(List<String> newDirtinessLevels) {
		if(newDirtinessLevels != null && ctrl_EditDirtiness != null){
			ctrl_EditDirtiness.setList(newDirtinessLevels);
			if(newDirtinessLevels == null || newDirtinessLevels.isEmpty()){
				addStateEvent(STATE_EVENT_DirtinessLevels_WARNING);
			}else{
				removeStateEvent(STATE_EVENT_DirtinessLevels_WARNING);
			}
		}				
	}
	
	@Override
	protected void updateAll(FacetItem facet) {		
		updateWeight(facet.getWeight());
		updateDescription(facet.getDescription());
		updateType(facet.getType());
		updateValueKreuzer(facet.getValueKreuzer());
		updateQualityMin(facet.getQualityMin());
		updateQualityMax(facet.getQualityMax());
		updateBrokennessLevels(facet.getBrokennessDescriptions());
		updateDirtinessLevels(facet.getDirtinessDescriptions());	
	}
	
	@Override
	protected void updateClearUI() {
		updateWeight(FacetItem.UNSET_WEIGHT);
		updateDescription(FacetItem.UNSET_DESCRIPTION);
		updateType(FacetItem.UNSET_TYPE);
		updateValueKreuzer(FacetItem.UNSET_VALUE_KREUZER);
		updateQualityMin(FacetItem.UNSET_QUALITY_MIN);
		updateQualityMax(FacetItem.UNSET_QUALITY_MAX);
		updateBrokennessLevels(new ArrayList<String>());
		updateDirtinessLevels(new ArrayList<String>());
	}
	/**
	 * helper to reform list suitable for modification of shapeness descriptions
	 * @param objectsList
	 * @return
	 */
	protected List<String> reformObjectListToStringList(List<Object> objectsList){
		List<String> descriptions = new ArrayList<String>();
		//reform transported list		
		for (Object object : objectsList) {
			if (object instanceof List<?>) {
				List<?> descriptionList = (List<?>) object;
				for (Object parameter : descriptionList) {
					if (parameter instanceof String) {
						String description = (String) parameter;
						descriptions.add(description);
					}
				}
			}					
		}
		return descriptions;
	}
	
	
	@Override	
	protected void preSetSection() {
		super.preSetSection();
		if(section != null){
			Control client = section.getClient();
			if (client instanceof CompositeEditItem) {
				CompositeEditItem compositeEditItem = (CompositeEditItem) client;
				deRegisterUIListeners(compositeEditItem);
			}
		}
	}

	@Override
	protected void postSetSection() {
		super.postSetSection();
		if(section != null){
			Control client = section.getClient();
			if (client instanceof CompositeEditItem) {
				compositeEditItem = (CompositeEditItem) client;
				registerUIListeners(compositeEditItem);
			}
		}
	}

	@Override
	protected void preSetNewFacet() {
		super.preSetNewFacet();
		if(facet !=  null){
			deRegisterModelListeners(facet);
		}
	}

	@Override
	protected void postSetNewFacet() {
		super.postSetNewFacet();
		if(facet !=  null){
			registerModelListeners(facet);
		}
	}

	
	
	protected void registerUIListeners(CompositeEditItem compositeEditItem) {
		compositeEditItem.registerUIListener(uilFacetItem);
		//wire brokenness sub-controllerLogView
		CompositeEditShapenessDescriptions cp_Brokenness = compositeEditItem.getCompositeBrokenness();
		if(cp_Brokenness != null){
			ControllerEditShapenessDescriptions ctrl_EditShapeness = getBrokennessSubControllerInstance();
			ctrl_EditShapeness.setComposite(cp_Brokenness);			
			celBrokenness = new CELEditShapenessDescriptionsBrokenness();
			ctrl_EditShapeness.registerEventListener(celBrokenness);			
		}
		//wire dirtiness sub-controllerLogView
		CompositeEditShapenessDescriptions cp_Dirtiness = compositeEditItem.getCompositeDirtiness();
		if(cp_Dirtiness != null){
			ControllerEditShapenessDescriptions ctrl_EditShapeness = getDirtinessSubControllerInstance();
			ctrl_EditShapeness.setComposite(cp_Dirtiness);			
			celDirtiness = new CELEditShapenessDescriptionsDirtiness();
			ctrl_EditShapeness.registerEventListener(celDirtiness);						
		}
	}


	protected void deRegisterUIListeners(CompositeEditItem compositeEditItem) {
		compositeEditItem.deRegisterUIListener(uilFacetItem);
		//un-wire brokenness sub-controllerLogView
		ControllerEditShapenessDescriptions ctrl_EditBrokenness = getBrokennessSubControllerInstance();
		ctrl_EditBrokenness.setComposite(null);
		ctrl_EditBrokenness.deRegisterEventListener(celBrokenness);
		//un-wire dirtiness sub-controllerLogView
		ControllerEditShapenessDescriptions ctrl_EditDirtiness = getDirtinessSubControllerInstance();
		ctrl_EditDirtiness.setComposite(null);
		ctrl_EditDirtiness.deRegisterEventListener(celDirtiness);
	}

	protected void registerModelListeners(FacetItem facet) {
		facet.getPropertyChangeSupport().addPropertyChangeListener(pclFacetItem);
	}

	protected void deRegisterModelListeners(FacetItem facet) {
		facet.getPropertyChangeSupport().removePropertyChangeListener(pclFacetItem);
	}
	
	protected ControllerEditShapenessDescriptions getBrokennessSubControllerInstance() {
		if(ctrl_EditBrokenness == null){
			ctrl_EditBrokenness = new ControllerEditShapenessDescriptions();
			ctrl_EditBrokenness.setAddDialogTitle(BROKENESS_ADD_DIALOG_TITLE);
			ctrl_EditBrokenness.setAddDialogMessage(BROKENESS_ADD_DIALOG_MESSAGE);			
		}
		return ctrl_EditBrokenness;
	}
	
	public void setBrokennessSubControllerInstance(ControllerEditShapenessDescriptions controllerBrokenness){
		ctrl_EditBrokenness = controllerBrokenness;
	}

	protected ControllerEditShapenessDescriptions getDirtinessSubControllerInstance() {
		if(ctrl_EditDirtiness == null){
			ctrl_EditDirtiness = new ControllerEditShapenessDescriptions();
			ctrl_EditDirtiness.setAddDialogTitle(DIRTINESS_ADD_DIALOG_TITLE);
			ctrl_EditDirtiness.setAddDialogMessage(DIRTINESS_ADD_DIALOG_MESSAGE);
		}
		return ctrl_EditDirtiness;
	}

	public void setDirtinessSubControllerInstance(ControllerEditShapenessDescriptions controllerDirtiness){
		ctrl_EditDirtiness = controllerDirtiness;
	}

	protected class UILFacetItem implements IUiChangeListener{

		@Override
		public void uiChanged(UiChangeEvent event) {
			Integer index = event.getIndex();
			switch (index) {
				case CompositeEditItem.IDX_WEIGHT:{
					String weight = (String)event.getNewValue();
					modifyWeight(weight);
					break;
				}
				case CompositeEditItem.IDX_TYPE:{
					String type = (String)event.getNewValue();
					modifyType(type);
					break;
				}
				case CompositeEditItem.IDX_QUALITY_MIN:{
					String quality = (String)event.getNewValue();
					modifyQualityMin(quality);
					break;
				}
				case CompositeEditItem.IDX_QUALITY_MAX:{
					String quality = (String)event.getNewValue();
					modifyQualityMax(quality);
					break;
				}
				case CompositeEditItem.IDX_VALUE_KREUZER:{
					String value = (String)event.getNewValue();
					modifyValueKreuzer(value);
					break;
				}
				case CompositeEditItem.IDX_DESCRIPTION:{
					String description = (String)event.getNewValue();
					modifyDescription(description);
					break;
				}
				default:
					logger.info("unknown uiChangeIndex: "+event.getIndex());
					break;
			}
		}

		@Override
		public void commandIssued(int index, Object... parameters) {
			//not used
		}
	}


	protected class PCLFacetItem implements PropertyChangeListener{
		@SuppressWarnings("unchecked")
		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			if (evt instanceof IndexedPropertyChangeEvent) {
				IndexedPropertyChangeEvent idxEvt = (IndexedPropertyChangeEvent) evt;
				Integer index = idxEvt.getIndex();
				switch (index) {
					case FacetItem.PI_NAME:{
						//do nothing as facet(item) name change does not need to be reflected in ItemComposite
						break;
					}
					case FacetItem.PI_WEIGHT:{
						Double newWeight = (Double)idxEvt.getNewValue();
						updateWeight(newWeight);
						break;
					}
					case FacetItem.PI_DESCRIPTION:{
						String newDescription = (String)idxEvt.getNewValue();
						updateDescription(newDescription);
						break;
					}
					case FacetItem.PI_TYPE:{
						String newTye = (String)idxEvt.getNewValue();
						updateType(newTye);
						break;
					}
					case FacetItem.PI_VALUE_KREUZER:{
						Long newValueKreuzer = (Long)idxEvt.getNewValue();
						updateValueKreuzer(newValueKreuzer);
						break;
					}
					case FacetItem.PI_QUALITY_MIN:{
						Double newQualityMin = (Double)idxEvt.getNewValue();
						updateQualityMin(newQualityMin);
						break;
					}
					case FacetItem.PI_QUALITY_MAX:{
						Double newQualityMax = (Double)idxEvt.getNewValue();
						updateQualityMax(newQualityMax);
						break;
					}
					case FacetItem.PI_BROKENNESS_LEVELS:{
						List<String> newBrokennessLevels = (List<String>)idxEvt.getNewValue();
						updateBrokennessLevels(newBrokennessLevels);
						break;
					}case FacetItem.PI_DIRTINESS_LEVELS:{
						List<String> newDirtinessLevels = (List<String>)idxEvt.getNewValue();
						updateDirtinessLevels(newDirtinessLevels);
						break;
					}
					case FacetItem.PI_BROKENNESS_PROPS:{
						IndexedPropertyChangeEvent subEvent = (IndexedPropertyChangeEvent)idxEvt.getNewValue();
						int subIndex = subEvent.getIndex();
						switch (subIndex) {
							case Shapeness.PI_SHAPENESS_DESCRIPTIONS:{
								List<String> newBrokennessLevels = (List<String>) subEvent.getNewValue();
								updateBrokennessLevels(newBrokennessLevels);
								break;							
							}								
						}						
						break;
					}case FacetItem.PI_DIRTINESS_PROPS:{
						IndexedPropertyChangeEvent subEvent = (IndexedPropertyChangeEvent)idxEvt.getNewValue();
						int subIndex = subEvent.getIndex();
						switch (subIndex) {
							case Shapeness.PI_SHAPENESS_DESCRIPTIONS:{
								List<String> newDirtinessLevels = (List<String>) subEvent.getNewValue();
								updateDirtinessLevels(newDirtinessLevels);
								break;							
							}								
						}						
						break;
					}


					default:
						logger.debug("unknown propertyChangeIndex: "+index);
						break;
				}
			}
		}
	}

	protected class CELEditShapenessDescriptionsBrokenness implements IListenerController{		
		@Override
		public int handleControllerEvent(ControllerEvent event) {
			if(ControllerEditShapenessDescriptions.CTRL_EVT_CODE_LIST_UPDATED.equals(event.getEventCode())){				
				modifyBrokennessDescriptions(event.getParameters());				
				return RETURN_HANDLED;
			}
			return RETURN_PROPAGATED;
		}
	}
	protected class CELEditShapenessDescriptionsDirtiness implements IListenerController{		
		@Override
		public int handleControllerEvent(ControllerEvent event) {
			if(ControllerEditShapenessDescriptions.CTRL_EVT_CODE_LIST_UPDATED.equals(event.getEventCode())){				
				modifyDirtinessDescriptions(event.getParameters());
				return RETURN_HANDLED;
			}
			return RETURN_PROPAGATED;
		}
	}	
}
